package ui.activity.login;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.LoginBean;
import student.andlei.com.student.R;
import ui.activity.MainActivity;
import utils.GsonUtils;
import utils.NetUtils;
import utils.SPUtils;
import utils.StringUtils;
import utils.log.LogUtil;


public class LoginActivity extends BaseNetActivity{
    private EditText ced_user_phone,
                            ced_password;
    private CheckBox checkBox;
    private TextView tv_register,
                     tv_forgetpassword;
    private Button bt_login;
    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        LoginBean Bean = (LoginBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(baseBean.data),LoginBean.class);
        if(baseBean.code==200){
            NetUtils.token = Bean.token;
            SPUtils.getInstance(this).put("user_phone",ced_user_phone.getText().toString().trim());
            SPUtils.getInstance(this).put("password",ced_password.getText().toString().trim());

            SPUtils.getInstance(this).put("token",Bean.token);
            SPUtils.getInstance(this).put("accid",Bean.accid);

            SPUtils.getInstance(this).put("school_id",Bean.school_id);
            SPUtils.getInstance(this).put("student_id",Bean.student_id);
            SPUtils.getInstance(this).put("school_status",Bean.school_status);
            LogUtil.app("accid---->"+SPUtils.getInstance(this).getString("accid"));
            LogUtil.app("token---->"+SPUtils.getInstance(this).getString("token"));
           // SPUtils.getInstance(this).put("grade_id",Bean.grade_id);
            if(Bean.degree_id==0){
                Bundle bundle = new Bundle();
                bundle.putString("degree_id=0","degree_id=0");
                bundle.putInt("student_id",Bean.student_id);
                bundle.putInt("school_status",Bean.school_status);
                jumpTo(RegisterNextActivity.class,bundle);
            }else {
                jumpTo(MainActivity.class);
                toast("登录成功");
            }

        }else {
            toast(""+baseBean.msg);
        }

    }

    @Override
    public void error(String action, Throwable e) {
        hideLoadDialog();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void findViews() {
        ced_user_phone = getView(R.id.ed_user);
        ced_password = getView(R.id.ed_password);
        checkBox = getView(R.id.checkbox_showpassword);
        tv_register = getView(R.id.tv_register);
        tv_forgetpassword = getView(R.id.tv_forget_the_password);
        bt_login = getView(R.id.bt_login);
    }

    @Override
    protected void formatViews() {
        bt_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_forgetpassword.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ced_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ced_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });

    }

    @Override
    protected void formatData() {
        if(!StringUtils.isNull(SPUtils.getInstance(context).getString("user_phone"))){
            ced_user_phone.setText(SPUtils.getInstance(context).getString("user_phone"));
        }
        if(!StringUtils.isNull(SPUtils.getInstance(context).getString("password"))){
            ced_password.setText(SPUtils.getInstance(context).getString("password"));
        }
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //注册
            case R.id.tv_register:
                jumpTo(RegisterActivity.class);
                break;
            //忘记密码
            case R.id.tv_forget_the_password:
                jumpTo(ForgetPasswordActivity.class);
                break;
            //登录
            case R.id.bt_login:
                Map<String,String> map = new HashMap<>();
                map.put("mobile",ced_user_phone.getText().toString().trim());
                map.put("password",ced_password.getText().toString().trim());
                post("common/login",map, LoginBean.class,true);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finishActivity();
    }
}
